package one.stand.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jmp.base.DynamicApi;
import com.jmp.base.dto.*;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageResponse2;
import one.stand.mapper.CommentMapperExt;
import one.stand.mapper.DynamicMapperExt;
import one.stand.model.Comment;
import one.stand.model.Dynamic;
import one.stand.model.ResultModel;
import one.stand.service.DynamicService;
import one.stand.util.SqlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * @author cjr
 */
@RestController
public class DynamicController implements DynamicApi {
    @Autowired
    private DynamicService dynamicService;
    @Autowired
    private DynamicMapperExt dynamicMapperExt;
    @Autowired
    private CommentMapperExt commentMapperExt;

    @Override
    public ResultModel save(@RequestBody DynamicSaveRequest request) {
        Dynamic dynamic = new Dynamic();
        dynamic.setDynamicId(request.getDynamicId());
        dynamic.setCompanyId(request.getCompanyId());
        dynamic.setType(request.getType());
        dynamic.setTitle(request.getTitle());
        dynamic.setDetails(request.getDetails());
        dynamic.setPhotos(request.getPhotos());
        dynamic.setRelateType(request.getRelateType());
        dynamic.setRelateId(request.getRelateId());
        dynamic.setCreateUserId(request.getCreateUserId());
        dynamicService.addDynamicInfo(dynamic);
        return ResultModel.success();
    }

    @Override
    public ResultModel updateByRelate(@RequestBody DynamicUpdateByRelateRequest request) {
        Dynamic dynamic = new Dynamic();
        dynamic.setTitle(request.getTitle());
        dynamic.setDetails(request.getDetails());
        dynamic.setPhotos(request.getPhotos());
        dynamicService.updateByRelate(request.getRelateType(), request.getRelateId(), dynamic);
        return ResultModel.success();
    }

    @Override
    public ResultModel remove(@RequestBody DynamicRemoveDto request) {
        dynamicService.delDynamicInfo(request.getDynamicId(), request.getOptUserId());
        return ResultModel.success();
    }

    @Override
    public ResultModel deleteByRelate(@RequestBody DynamicDeleteRelateRequest request) {
        dynamicService.deleteByRelate(request.getRelateType(), request.getRelateId());
        return ResultModel.success();
    }

    @Override
    public ResultModel<DynamicCommentListVo> commentList(@RequestBody DynamicCommentListDto request) {

        Integer rootId = Objects.nonNull(request.getParentId()) ? request.getParentId() : 0;
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<Comment> commentList = commentMapperExt.selectList(request.getCommentType(), request.getDynamicId(), rootId);
        PageResponse2<DynamicCommentListVo.Comment> commentPageResponse2 = ConvertUtils.page2(commentList, comment -> {

            Integer coverNum = 0;
            if (rootId == 0) {
                PageHelper.startPage(1, 1);
                List<Comment> list = commentMapperExt.selectList(request.getCommentType(), request.getDynamicId(), comment.getCommentId());
                coverNum = Math.toIntExact(((Page) list).getTotal());
            }

            return DynamicCommentListVo.Comment.builder()
                    .commentId(comment.getCommentId())
                    .userId(comment.getUserId())
                    .coverUserId(comment.getCoverUserId())
                    .content(comment.getText())
                    .createTime(comment.getCreateTime().getTime())
                    .thumbsNum(comment.getThumbsNum())
                    .coverNum(coverNum)
                    .build();
        });

        DynamicCommentListVo vo = DynamicCommentListVo.builder()
                .commentPage(commentPageResponse2)
                .build();
        return ResultModel.success(vo);
    }

    @Override
    public ResultModel<DynamicCommentSaveVo> commentSave(@RequestBody DynamicCommentSaveDto request) {

        DynamicCommentSaveVo vo = dynamicService.commentSave(request.getCommentType(), request.getDynamicId(), request.getParentId(),
                request.getUserId(), request.getText(), request.getCardId());

        return ResultModel.success(vo);
    }

    @Override
    public ResultModel commentRemove(@RequestBody DynamicCommentRemoveDto request) {
        for (Integer commentId : request.getCommentIds()) {
            dynamicService.commentRemove(commentId, request.getUserId());
        }

        return ResultModel.success();
    }

    @Override
    public ResultModel commentThumbs(@RequestBody DynamicCommentThumbsDto request) {

        dynamicService.commentThumbs(request.getCommentId(), request.getThumbsFlag());

        return ResultModel.success();
    }

    @Override
    public ResultModel<DynamicListVo> list(@RequestBody DynamicListDto request) {

        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<Dynamic> dynamics = dynamicMapperExt.selectList(request.getCompanyId(), request.getUserId(), SqlUtil.like(request.getKeyword()), request.getViewDate());
        PageResponse2<DynamicListVo.Dynamic> dynamicPageResponse2 = ConvertUtils.page2(dynamics, dynamic -> {
            return DynamicListVo.Dynamic.builder()
                    .dynamicId(dynamic.getDynamicId())
                    .companyId(dynamic.getCompanyId())
                    .createUserId(dynamic.getCreateUserId())
                    .time(dynamic.getCreateTime().getTime())
                    .type(dynamic.getType())
                    .title(dynamic.getTitle())
                    .photos(dynamic.getPhotos())
                    .detail(dynamic.getDetails())
                    .build();
        });

        DynamicListVo vo = DynamicListVo.builder()
                .dynamics(dynamicPageResponse2)
                .build();
        return ResultModel.success(vo);
    }
}
